#include "Library.h"
#include "LibraryRecord.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "../lib/rapidxml/rapidxml.hpp"

LibraryRecord Library::getRecord(int index) {
	return lib.at(index);
}

void Library::addRecord(LibraryRecord record) {
	lib.push_back(record);
}

void Library::removeRecord(int index) {
	swap(lib.at(index), lib.back());
	lib.pop_back();
}

Library::Library() {
	std::ifstream libStream("lib.txt");
	libStream.seekg(0, std::ios::end);
	size_t length = libStream.tellg();
	libStream.seekg(0, std::ios::beg);
	char * buf = new char[length + 1];
	libStream.read(buf,length);
	buf[length] = '\0';
	libStream.close();
	
	rapidxml::xml_document<> doc;
	doc.parse<0>(buf);
	libNode = doc.first_node("lib");
	for (rapidxml::xml_node<> * n = libNode->first_node("book"); n; n = n->next_sibling()) 		{
		std::cout << "TIC!";
		LibraryRecord lr(n);
		addRecord(lr);
	}	
	
	//removeRecord(getSize() - 1);
}

int Library::getSize() {
	return lib.size();
}
